package com.bosssoft.trainee.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.trainee.common.annotation.Description;
import com.bosssoft.trainee.mbg.dto.UserDTO;
import com.bosssoft.trainee.mbg.po.User;
import com.bosssoft.trainee.mbg.vo.TokenVO;
import com.bosssoft.trainee.mbg.vo.UserVO;
import com.bosssoft.trainee.mbg.result.CommonResult;
import com.bosssoft.trainee.service.UserService;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;


/**
 * @Author: wuxin
 * @Date: 2022/6/17
 * @Project rbac
 * @Version 1.0
 * @Description
 */
@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    UserService userService;

    @Description("登录并获取token")
    @PostMapping(value = "/login")
    public CommonResult<TokenVO> login(@RequestBody UserVO userVO) {
        String token = userService.login(userVO.getUsername(), userVO.getPassword());
        if (token == null) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        TokenVO tokenVO = new TokenVO(token, tokenHead);
        return CommonResult.success(tokenVO);
    }

    @Description("获取用户列表")
    @GetMapping(value = "/list")
    public CommonResult<Page<User>> getUserList(@RequestParam(value = "example", required = false) UserVO userVO,
                                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                @RequestParam(value = "pageNum", defaultValue = "1") int pageNum) {
        UserDTO example = new UserDTO();
        BeanUtil.copyProperties(userVO, example);
        return CommonResult.success(userService.getList(pageSize, pageNum, example));
    }

    @Description("根据id删除用户")
    @GetMapping(value = "/deleteById")
    public CommonResult<String> deleteById(@RequestParam("id") int id) {
        return userService.deleteById(id) > 0 ? CommonResult.success("删除成功") : CommonResult.failed("删除失败");
    }

    @Description("新增用户")
    @PostMapping(value = "/addUser")
    public CommonResult<String> addUser(@Validated @RequestBody UserVO userVO) {
        UserDTO userDTO = new UserDTO();
        BeanUtil.copyProperties(userVO, userDTO);
        return userService.addUser(userDTO) > 0 ? CommonResult.success("添加成功") : CommonResult.failed("添加失败");
    }

    @Description("修改用户")
    @PostMapping(value = "/updateUser")
    public CommonResult<String> updateUser(@Validated @RequestBody UserVO userVO) {
        if (userVO.getId() == null) {
            return CommonResult.failed("修改失败,id不能为空");
        }
        UserDTO userDTO = new UserDTO();
        BeanUtil.copyProperties(userVO, userDTO);
        return userService.updateUser(userDTO) > 0 ? CommonResult.success("修改成功") : CommonResult.failed("修改失败");
    }

    @Description("检查工号是否被占用")
    @GetMapping(value = "checkWorkerNum")
    public CommonResult<Boolean> checkWorkerNum(@NotBlank(message = "工号不能为空")
                                                @Pattern(regexp = "^[A-Z]{2}\\d{6}$", message = "工号格式不正确")
                                                @RequestParam("workerNum") String workerNum) {
        return CommonResult.success(userService.checkWorkerNum(workerNum));
    }

    @Description("检查用户名是否被占用")
    @GetMapping(value = "checkUsername")
    public CommonResult<Boolean> checkUsername(@NotBlank(message = "用户名不能为空")
                                               @Length(min = 4, max = 16, message = "用户名长度在 4 到 16 个字符")
                                               @Pattern(regexp = "^[a-zA-Z\\d_-]{4,16}$", message = "用户名只能由字母、数字、\"_\"、\"-\"组成")
                                               @RequestParam("username") String username) {
        return CommonResult.success(userService.checkUsername(username));
    }
}
